package com.firexxx.study.lock;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.firexxx.study.lock.task.AtomicConsumeTask;
import com.firexxx.study.lock.task.LockConsumeTask;
import com.firexxx.study.lock.task.SyncConsumeTask;
import com.firexxx.study.performancetest.scene.FixedTimeScene;
import com.firexxx.study.performancetest.scene.IResult;
import com.firexxx.study.performancetest.scene.MultiResult;
import com.firexxx.study.performancetest.task.IFixedTimeConsumeTask;
import com.firexxx.study.performancetest.timeconsume.ConsumeTaskType;
import com.firexxx.study.performancetest.timeconsume.ConsumeTimeArg;

import java.util.HashMap;
import java.util.Map;

/**
 * 锁性能测试
 *
 * @author fireXXX
 * @date 2020/11/14 23:12
 **/
public class TestSyncAndLockPerformance {

    private static Map<String, IFixedTimeConsumeTask> init() {
        Map<String, IFixedTimeConsumeTask> tasks = new HashMap<>();
        //    private static IFixedTimeConsumeTask nullTask = new NullConsumeTask();
        IFixedTimeConsumeTask syncTask = new SyncConsumeTask();
        IFixedTimeConsumeTask lockTask = new LockConsumeTask();
        IFixedTimeConsumeTask atomicTask = new AtomicConsumeTask();

//        tasks.put("null", nullTask);
        tasks.put("sync", syncTask);
        tasks.put("lock", lockTask);
        tasks.put("atomic", atomicTask);

        return tasks;
    }

    private static void test(int thread, int sleepTime, int sample, ConsumeTaskType consumeTaskType) throws InterruptedException, IllegalAccessException, InstantiationException {
        Map<String, IFixedTimeConsumeTask> tasks = init();

        ConsumeTimeArg arg = new ConsumeTimeArg(sleepTime, consumeTaskType);
        tasks.forEach((k, v) -> {
            v.init(arg);
        });

        String sceneName = thread + "-" + sleepTime + "-" + consumeTaskType.toString();
        FixedTimeScene fixedTimeScene = new FixedTimeScene();
        IResult result = fixedTimeScene.startSync(sceneName, thread, sample, tasks, MultiResult.class);
        result.show(sceneName);
    }

    public static void main(String[] args) throws InterruptedException, InstantiationException, IllegalAccessException, JsonProcessingException {
        //1，2，5，10，20个线程休眠16ms
//        test(1, 16, 10, ConsumeTaskType.io);
        test(2, 16, 10, ConsumeTaskType.io);
        test(5, 16, 10, ConsumeTaskType.io);
        test(10, 16, 10, ConsumeTaskType.io);
        test(20, 16, 10, ConsumeTaskType.io);
        //1，2，5，10，20个线程休眠128ms
        test(1, 128, 10, ConsumeTaskType.io);
        test(2, 128, 10, ConsumeTaskType.io);
        test(5, 128, 10, ConsumeTaskType.io);
        test(10, 128, 10, ConsumeTaskType.io);
        test(20, 128, 10, ConsumeTaskType.io);

        //1，2，5，10，20个线程休眠16ms
        test(1, 16, 10, ConsumeTaskType.cpu);
        test(2, 16, 10, ConsumeTaskType.cpu);
        test(5, 16, 10, ConsumeTaskType.cpu);
        test(10, 16, 10, ConsumeTaskType.cpu);
        test(20, 16, 10, ConsumeTaskType.cpu);
        //1，2，5，10，20个线程休眠128ms
        test(1, 128, 10, ConsumeTaskType.cpu);
        test(2, 128, 10, ConsumeTaskType.cpu);
        test(5, 128, 10, ConsumeTaskType.cpu);
        test(10, 128, 10, ConsumeTaskType.cpu);
        test(20, 128, 10, ConsumeTaskType.cpu);
    }

}